package com.wyl.mine.unique

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.TYPE_CHILD
import com.wyl.base.TYPE_GROUP
import com.wyl.base.databinding.CommonListActivityBinding
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewSpace
import com.wyl.libbase.extend.dp2px
import com.wyl.mine.R
import io.ditclear.bindingadapter.MultiTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel

class AuthenticationActivity : BindingActivity<CommonListActivityBinding>() {
    private val viewModel by viewModel<AuthenticationViewModel>()

    override fun getLayoutId(): Int = R.layout.common_list_activity

    override fun loadData() {
        binding.include.refreshLayout.startRefresh()
    }

    override fun initView() {
        binding.vm = viewModel

        binding.titleBar.apply {
            centerTextView.text = "认证列表"
            setListener { _, action, _ ->
                when (action) {
                    CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                    else -> {
                    }
                }
            }
        }

        binding.ad.visibility = View.GONE

        binding.include.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3).apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int =
                        when (mAdapter.getItemViewType(position)) {
                            TYPE_GROUP -> 3
                            else -> 1
                        }
                }
            }
            adapter = mAdapter
            addItemDecoration(RecyclerViewSpace(dp2px(16f)))
        }
    }

    private val mAdapter by lazy {
        TYPE_GROUP
        MultiTypeAdapter(
            this,
            viewModel.dataSource,
            object : MultiTypeAdapter.MultiViewTyper {
                override fun getViewType(item: Any): Int = when (item) {
                    is String -> TYPE_GROUP
                    else -> TYPE_CHILD
                }
            }).apply {
            addViewTypeToLayoutMap(TYPE_GROUP, R.layout.authentic_item_group)
            addViewTypeToLayoutMap(TYPE_CHILD, R.layout.service_item1)
            //  itemPresenter = o
        }
    }
}
